<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class Printer extends \app\BaseModel
{
    protected $name = "massage_service_printer";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        if(empty($data)) {
            $this->dataAdd($dis);
            $data = $this->where($dis)->find();
        }
        return !empty($data) ? $data->toArray() : [];
    }
    public function userOrder($id)
    {
        $order_model = new Order();
        $order = $order_model->dataInfo(["id" => $id]);
        $brs = "<BR>";
        $orderInfo = "<CB>订单小票</CB><BR>";
        $orderInfo .= "--------------------------------" . $brs;
        $orderInfo .= "服务/数量/价格" . $brs;
        $orderInfo .= "--------------------------------" . $brs;
        foreach ($order["order_goods"] as $v) {
            $v["goods_name"] = mb_convert_encoding($v["goods_name"], "UTF-8", "auto");
            $orderInfo .= $v["goods_name"] . $brs;
            $orderInfo .= "  X" . $v["num"] . "               " . round($v["true_price"] * $v["num"], 2) . "元" . $brs;
            $orderInfo .= "" . $brs;
            $orderInfo .= "" . $brs;
        }
        $coach_model = new Coach();
        $address_model = new OrderAddress();
        $coach_name = $coach_model->where(["id" => $order["coach_id"]])->value("coach_name");
        $user_name = $address_model->where(["order_id" => $order["id"]])->value("user_name");
        $orderInfo .= "预约技师:" . $coach_name . $brs;
        $orderInfo .= "下单人:" . $user_name . $brs;
        $time = date("Y-m-d H:i", $order["start_time"]) . "-" . date("Y-m-d H:i", $order["end_time"]);
        $orderInfo .= "预约时间:" . $time . $brs;
        $orderInfo .= "预约金额:" . round($order["pay_price"], 2) . $brs;
        $orderInfo .= "付款时间:" . date("Y-m-d H:i", $order["pay_time"]) . $brs;
        return $orderInfo;
    }
    public function storeOrderPrinter($uniacid, $orderInfo, $aotu = 1)
    {
        $dis = ["uniacid" => $uniacid, "status" => 1];
        if($aotu == 1) {
            $dis["auto"] = 1;
        }
        $printer_config = $this->where($dis)->select()->toArray();
        if(!empty($printer_config)) {
            foreach ($printer_config as $value) {
                $value["ukey"] = $value["api_key"];
                $value["sn"] = $value["printer_key"];
                $res = \longbingcore\printer\Printer::FeiePrintMsg($value, $orderInfo, "Open_printMsg", $value["user_ticket_num"]);
            }
        }
        return true;
    }
    public function printer($id, $aotu = 1)
    {
        $user_order = $this->userOrder($id);
        $order_model = new Order();
        $uniacid = $order_model->where(["id" => $id])->value("uniacid");
        $res = $this->storeOrderPrinter($uniacid, $user_order, $aotu);
        return $res;
    }
    public function cancelOrderPrinter()
    {
    }
}

?>